package user

import (
	"context"
	"github.com/jinzhu/copier"

	"large-model-platform-backend/internal/svc"
	"large-model-platform-backend/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ChatSessionMessagesLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewChatSessionMessagesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatSessionMessagesLogic {
	return &ChatSessionMessagesLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ChatSessionMessagesLogic) ChatSessionMessages(req *types.ChatSessionReq) (resp *types.UserSingleResp, err error) {
	model := l.svcCtx.UserModel
	user, err := model.FindOne(l.ctx, req.UserId)

	//todo 后续使用mongo 语法过滤指定 数组
	var chats []types.ChatSession
	for _, session := range user.ChatSessions {
		if session.Name != req.Name {
			continue
		}
		var messages []types.Message
		for _, message := range session.Messages {
			t := types.Message{}
			copier.Copy(&t, &message)
			messages = append(messages, t)
		}
		chats = append(chats, types.ChatSession{
			Name:     session.Name,
			Messages: messages,
		})
	}

	u := types.User{}
	copier.Copy(&u, user)
	u.Password = "*******"
	t := types.UserData{
		ID:           user.ID.Hex(),
		User:         u,
		ChatSessions: chats,
	}

	return &types.UserSingleResp{
		Result: types.Result{Code: 200},
		Data:   t,
	}, nil
}
